﻿using System;
using System.Data;
using System.Data.SQLite;
using System.Text;

using Stock.DBUtility;
using Stock.Model;

namespace Stock.Dal
{
    /// <summary>
    /// 数据访问类:MyStockDal
    /// </summary>
    public partial class MyStockDal
    {
        public MyStockDal()
        { }

        #region Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string S_Code, string User_Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from MyStockInfo");
            strSql.Append(" where S_Code=@S_Code and User_Code=@User_Code");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@S_Code", DbType.String),
                                           new SQLiteParameter("@User_Code", DbType.String)};
            parameters[0].Value = S_Code;
            parameters[1].Value = User_Code;
            return DbHelperSQLite.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(MyStockEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into MyStockInfo(");
            strSql.Append("S_Code,User_Code,Copies,InAmount,OutAmount,FeeAmount,LastPrice,AvgPrice)");
            strSql.Append(" values (");
            strSql.Append("@S_Code,@User_Code,@Copies,@InAmount,@OutAmount,@FeeAmount,@LastPrice,@AvgPrice)");
            strSql.Append(";select LAST_INSERT_ROWID()");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@S_Code", DbType.Int32,8),
					new SQLiteParameter("@User_Code", DbType.String,20),
					new SQLiteParameter("@Copies", DbType.Int32,8),
					new SQLiteParameter("@InAmount", DbType.Decimal,8),
					new SQLiteParameter("@OutAmount", DbType.Int32,8),
					new SQLiteParameter("@FeeAmount", DbType.Decimal,8),
					new SQLiteParameter("@LastPrice", DbType.Decimal,8),
					new SQLiteParameter("@AvgPrice", DbType.Decimal,8)};
            parameters[0].Value = model.S_Code;
            parameters[1].Value = model.User_Code;
            parameters[2].Value = model.Copies;
            parameters[3].Value = model.InAmount;
            parameters[4].Value = model.OutAmount;
            parameters[5].Value = model.FeeAmount;
            parameters[6].Value = model.LastPrice;
            parameters[7].Value = model.AvgPrice;

            object obj = DbHelperSQLite.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(MyStockEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update MyStockInfo set ");
            strSql.Append("S_Code=@S_Code,");
            strSql.Append("User_Code=@User_Code,");
            strSql.Append("Copies=@Copies,");
            strSql.Append("InAmount=@InAmount,");
            strSql.Append("OutAmount=@OutAmount,");
            strSql.Append("FeeAmount=@FeeAmount,");
            strSql.Append("LastPrice=@LastPrice,");
            strSql.Append("AvgPrice=@AvgPrice");
            strSql.Append(" where My_Stock_Code=@My_Stock_Code");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@S_Code", DbType.Int32,8),
					new SQLiteParameter("@User_Code", DbType.String,20),
					new SQLiteParameter("@Copies", DbType.Int32,8),
					new SQLiteParameter("@InAmount", DbType.Decimal,8),
					new SQLiteParameter("@OutAmount", DbType.Int32,8),
					new SQLiteParameter("@FeeAmount", DbType.Decimal,8),
					new SQLiteParameter("@LastPrice", DbType.Decimal,8),
					new SQLiteParameter("@AvgPrice", DbType.Decimal,8),
					new SQLiteParameter("@My_Stock_Code", DbType.Int32,8)};
            parameters[0].Value = model.S_Code;
            parameters[1].Value = model.User_Code;
            parameters[2].Value = model.Copies;
            parameters[3].Value = model.InAmount;
            parameters[4].Value = model.OutAmount;
            parameters[5].Value = model.FeeAmount;
            parameters[6].Value = model.LastPrice;
            parameters[7].Value = model.AvgPrice;
            parameters[8].Value = model.My_Stock_Code;

            int rows = DbHelperSQLite.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MyStockEntity GetModel(int My_Stock_Code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select My_Stock_Code,S_Code,User_Code,Copies,InAmount,OutAmount,FeeAmount,LastPrice,AvgPrice from MyStockInfo ");
            strSql.Append(" where My_Stock_Code=@My_Stock_Code");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@My_Stock_Code", DbType.Int32,4)
};
            parameters[0].Value = My_Stock_Code;

            MyStockEntity model = new MyStockEntity();
            DataSet ds = DbHelperSQLite.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["My_Stock_Code"] != null && ds.Tables[0].Rows[0]["My_Stock_Code"].ToString() != "")
                {
                    model.My_Stock_Code = int.Parse(ds.Tables[0].Rows[0]["My_Stock_Code"].ToString());
                }
                if (ds.Tables[0].Rows[0]["S_Code"] != null && ds.Tables[0].Rows[0]["S_Code"].ToString() != "")
                {
                    model.S_Code = ds.Tables[0].Rows[0]["S_Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["User_Code"] != null && ds.Tables[0].Rows[0]["User_Code"].ToString() != "")
                {
                    model.User_Code = ds.Tables[0].Rows[0]["User_Code"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Copies"] != null && ds.Tables[0].Rows[0]["Copies"].ToString() != "")
                {
                    model.Copies = int.Parse(ds.Tables[0].Rows[0]["Copies"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InAmount"] != null && ds.Tables[0].Rows[0]["InAmount"].ToString() != "")
                {
                    model.InAmount = decimal.Parse(ds.Tables[0].Rows[0]["InAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OutAmount"] != null && ds.Tables[0].Rows[0]["OutAmount"].ToString() != "")
                {
                    model.OutAmount = int.Parse(ds.Tables[0].Rows[0]["OutAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FeeAmount"] != null && ds.Tables[0].Rows[0]["FeeAmount"].ToString() != "")
                {
                    model.FeeAmount = decimal.Parse(ds.Tables[0].Rows[0]["FeeAmount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["LastPrice"] != null && ds.Tables[0].Rows[0]["LastPrice"].ToString() != "")
                {
                    model.LastPrice = decimal.Parse(ds.Tables[0].Rows[0]["LastPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AvgPrice"] != null && ds.Tables[0].Rows[0]["AvgPrice"].ToString() != "")
                {
                    model.AvgPrice = decimal.Parse(ds.Tables[0].Rows[0]["AvgPrice"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select My_Stock_Code,S_Code,User_Code,Copies,InAmount,OutAmount,FeeAmount,LastPrice,AvgPrice ");
            strSql.Append(" FROM MyStockInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQLite.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@tblName", DbType.VarChar, 255),
                    new SQLiteParameter("@fldName", DbType.VarChar, 255),
                    new SQLiteParameter("@PageSize", DbType.Int32),
                    new SQLiteParameter("@PageIndex", DbType.Int32),
                    new SQLiteParameter("@IsReCount", DbType.bit),
                    new SQLiteParameter("@OrderType", DbType.bit),
                    new SQLiteParameter("@strWhere", DbType.VarChar,1000),
                    };
            parameters[0].Value = "MyStockInfo";
            parameters[1].Value = "My_Stock_Code";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;
            return DbHelperSQLite.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion Method
    }
}